package exclude

type Region struct {
}

var (
	region *Region
)

func init() {
	region = &Region{}
}

type RegionChildName struct {
	Id       int    `json:"id"`
	Name     string `json:"name"`
	ParentId int    `json:"parentId"`
}

func (r *Region) QueryRegion(db *DB) map[string]interface{} {
	//读取洲
	var sql = "SELECT id,provinceName FROM provinceInfo ORDER BY id"
	rows, err := db.AdminDB.Query(sql)
	if err != nil {
		return nil
	}
	defer rows.Close()
	var list []*RegionChildName
	for rows.Next() {
		info := &RegionChildName{}
		if err := rows.Scan(&info.Id, &info.Name); err != nil {
			continue
		}
		info.ParentId = 0
		list = append(list, info)
	}
	//读取分区
	var sqlArea = "SELECT id,areaName FROM areaInfo ORDER BY id"
	rowsArea, err := db.AdminDB.Query(sqlArea)
	if err != nil {
		return nil
	}
	defer rowsArea.Close()
	var listArea []*RegionChildName
	for rowsArea.Next() {
		info := &RegionChildName{}
		if err := rowsArea.Scan(&info.Id, &info.Name); err != nil {
			continue
		}
		info.ParentId = info.Id / 10000 * 10000
		listArea = append(listArea, info)
	}
	//读取过去
	var sqlCity = "SELECT id,concat(cityName," + "'('" + ",cityCode," + " ')' " + ")" + " as cityName  FROM cityInfo ORDER BY id"
	rowsCity, err := db.AdminDB.Query(sqlCity)
	if err != nil {
		return nil
	}
	defer rowsCity.Close()
	var listCity []*RegionChildName
	for rowsCity.Next() {
		info := &RegionChildName{}
		if err := rowsCity.Scan(&info.Id, &info.Name); err != nil {
			continue
		}
		info.ParentId = info.Id / 100 * 100
		listCity = append(listCity, info)
	}
	ret := make(map[string]interface{})
	ret["Region1"] = list
	ret["Region2"] = listArea
	ret["Region3"] = listCity
	return ret
}
